package com.cloudcc.mobile.view.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;

import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.approval.TaskManagementModel;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.web.CloudWebViewActivity;
import com.cloudcc.mobile.view.web.WebSyncDefaultProxy;
import com.cloudcc.mobile.view.web.WebSyncDefaultProxy.MonitorParam;

/**
 * 审批(待办任务详细)
 * 
 * @author lhy
 * @date 2015/12/09/17:00
 */
public class TaskManagementItemActivty extends BaseActivity implements
		OnClickListener {
	private TaskManagementModel model = null;
	private String ACTION = "com.cloudcc.mobile.syncWeb";
	@Bind(R.id.bar_back)
	ImageView back;
	@Bind(R.id.bar_title_text)
	TextView title;
	@Bind(R.id.bar_button_plus)
	ImageView imgplus;
	@Bind(R.id.mgt_approval_titlename)
	TextView titlename;
	@Bind(R.id.mgt_approval_lay)
	LinearLayout approval_lay_type;
	@Bind(R.id.mgt_approval_type)
	TextView approval_type;
	@Bind(R.id.mgt_sponsor_man)
	TextView sponsor;
	@Bind(R.id.mgt_approval_content)
	TextView content;
	@Bind(R.id.mgt_sponsor_time)
	TextView sponsor_time;
	@Bind(R.id.again_allocation)
	// 重新分配
	LinearLayout allocation;
	@Bind(R.id.going_approval)
	// 进行审批
	LinearLayout approval;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		back.setOnClickListener(this);
		title.setText("待审批");
		imgplus.setVisibility(View.GONE);
		approval_lay_type.setOnClickListener(this);
		allocation.setOnClickListener(this);
		approval.setOnClickListener(this);
		initdata();
		initReceiver();
	}

	public void initdata() {
		if (model == null) {
			model = (TaskManagementModel) getIntent().getSerializableExtra(
					"task");
		}
		titlename.setText(model.getObjtype());
		approval_type.setText("待审批");
		sponsor.setText(model.getActor());
		content.setText(model.getObjtype() + model.getObjname());
		sponsor_time.setText(model.getActdate());
	}

	public void initReceiver() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ACTION);
		registerReceiver(receiver, intentFilter);
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ACTION)) {
				TaskManagementItemActivty.this.setResult(Activity.RESULT_OK,
						null);
				TaskManagementItemActivty.this.finish();
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.management_approval_desc;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_back:
			TaskManagementItemActivty.this.finish();
			break;
		case R.id.mgt_approval_lay:
			Intent intent = new Intent(TaskManagementItemActivty.this,
					TaskManagementHistoryActivty.class);
			intent.putExtra("objecttype", model.getObjtype());
			intent.putExtra("relatedId", model.getObjid());
			intent.putExtra("title", "审批");
			startActivity(intent);
			break;
		case R.id.again_allocation:
			Intent intent1 = new Intent(mContext, CloudWebViewActivity.class);
			intent1.putExtra(ExtraConstant.EXTRA_URL, UrlManager.getManager()
					.getReallocate(model.getWorkItemid(), model.getObjid()));
			MonitorParam monitorParam = new MonitorParam(
					new String[] { "WeiXin_reassignWorkitem.action" },
					WebSyncDefaultProxy.STEP_START,
					new String[] { "WeiXin_index.action" }, true,
					WebSyncDefaultProxy.STEP_FINISH);
			intent1.putExtra("callback",
					WebSyncDefaultProxy.creatDefaultProxy(ACTION)
							.addMonitorParam(monitorParam).addHomeMonitor());
			startActivity(intent1);
			break;
		case R.id.going_approval:
			Intent intent2 = new Intent(mContext, CloudWebViewActivity.class);
			intent2.putExtra(ExtraConstant.EXTRA_URL, UrlManager.getManager()
					.getGoApproval(model.getWorkItemid(), model.getObjid()));
			MonitorParam monitorParam2 = new MonitorParam(new String[] {
					"/WeiXin_processApproval.action", "allocationSp.action" },
					WebSyncDefaultProxy.STEP_START,
					new String[] { "WeiXin_index.action" }, true,
					WebSyncDefaultProxy.STEP_FINISH);
			intent2.putExtra("callback",
					WebSyncDefaultProxy.creatDefaultProxy(ACTION)
							.addMonitorParam(monitorParam2).addHomeMonitor());
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

}
